//
//  VideoView.m
//  Rail
//
//  Created by huyuchen on 2018/3/15.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "VideoView.h"
#import "PlayRateTableViewCell.h"
#import "PopoverView.h"

#pragma mark VideoStreamModel-----------------
@implementation VideoStreamModel

@end

#pragma mark VideoPlayView-----------------
@interface VideoPlayView () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) VideoToolBar *toolBar;
@property (nonatomic, strong) BMCustomButton *selectDeviceButton;
@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, assign) BOOL isMovingTableView;
@property (nonatomic, assign) BOOL isShowStreamTableView;
@property (nonatomic, assign) BOOL isTap;

@property (nonatomic, strong) VideoIpTypeModel *unSelectIpModel;

@end
@implementation VideoPlayView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.clipsToBounds = YES;
        [self addSubview:self.toolBar];
        [self.toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.mas_equalTo(40);
        }];
        
        BMWeakSelf
        [self.toolBar setBlockClickStreamBtn:^{
            //移动
            [weakSelf moveStreamTableView];
        }];
        
        [self addSubview:self.playContainer];
        [self.playContainer mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.bottom.equalTo(self.toolBar.mas_top);
        }];
        [self addSubview:self.tableView];
        self.tableView.frame = CGRectMake(frame.size.width, 0, 110, frame.size.height);
        [self addSubview:self.selectDeviceButton];
        [self.selectDeviceButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.playContainer);
        }];
        
        [self addSubview:self.playButton];
        [self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.playContainer);
        }];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.tableView.frame = CGRectMake(self.frame.size.width, 0, 110, self.frame.size.height-40);
}

- (void)selectDeviceButtonDidClick {
    if ([self.videoView.delegate respondsToSelector:@selector(videoView:videoPlayView:clickPlayWithIndex:count:)]) {
        [self.videoView.delegate videoView:self.videoView videoPlayView:self clickPlayWithIndex:self.index count:[self.videoView videoPlayCount]];
    }
}

- (void)playButtonDidClick {
    if ([self.videoView.delegate respondsToSelector:@selector(videoView:videoPlayView:clickPlayPauseButton:playStatus:)]) {
        [self.videoView.delegate videoView:self.videoView videoPlayView:self clickPlayPauseButton:self.toolBar.playBtn playStatus:self.playStatus];
    }
}

- (void)moveStreamTableView {
    if (self.isMovingTableView) {
        return;
    }
    
    [self bringSubviewToFront:self.tableView];
    self.isMovingTableView = YES;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (CGRectGetMinX(self.tableView.frame) < self.frame.size.width - 10) {
            CGRect frame = self.tableView.frame;
            frame.origin.x = self.frame.size.width + 3;
            //隐藏
            self.isShowStreamTableView = NO;
            [UIView animateWithDuration:0.25f animations:^{
                self.tableView.frame = frame;
            } completion:^(BOOL finished) {
                self.isMovingTableView = NO;
            }];
        }
        else {
            CGRect frame = self.tableView.frame;
            frame.origin.x = self.frame.size.width - 110;
            //显示
            self.isShowStreamTableView = YES;
            [UIView animateWithDuration:0.25f animations:^{
                self.tableView.frame = frame;
            } completion:^(BOOL finished) {
                self.isMovingTableView = NO;
            }];
        }
    });

}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.isTap) {
        [self showPopover];
    }
}

#pragma mark - Popover
- (void)addPopover {
    self.isTap = YES;
}

- (void)removePopover {
    self.isTap = NO;
}

- (void)showPopover {
    PopoverView *popoverView = [PopoverView popoverView];
    popoverView.arrowStyle = PopoverViewArrowStyleTriangle;
    
    CGFloat height = [self actions].count * 50;
    height = height > ScreenWidth - 60 ? ScreenWidth - 60 : height;
    CGFloat width = 180;
    CGFloat originY = (ScreenHeight - height)*0.5;
    CGFloat originX = (ScreenWidth - width)*0.5;
    
    [popoverView showInWindowWithFrame:CGRectMake(originX, originY, width, height) withActions:[self actions]];
    [popoverView setRotation:-M_PI_2];
}

#pragma mark 分屏弹窗
- (NSArray<PopoverAction *> *)actions {
    //1.停止/播放
    BMWeakSelf
    NSString *playTitle = self.playStatus == PlayVideoStatusPlaying?@"停止":@"播放";
    PopoverAction *playAction = [PopoverAction actionWithTitle:playTitle handler:^(PopoverAction *action) {
        if ([weakSelf.videoView.delegate respondsToSelector:@selector(videoView:videoPlayView:clickPlayPauseButton:playStatus:)]) {
            [weakSelf.videoView.delegate videoView:weakSelf.videoView videoPlayView:weakSelf clickPlayPauseButton:weakSelf.toolBar.playBtn playStatus:weakSelf.playStatus];
        }
    }];
    
    //2.选择播放设备
    PopoverAction *selectAction = [PopoverAction actionWithTitle:@"选择播放设备" handler:^(PopoverAction *action) {
        if ([weakSelf.videoView.delegate respondsToSelector:@selector(videoView:videoPlayView:splitedScreenSelectVideoWithIndex:)]) {
            [weakSelf.videoView.delegate videoView:weakSelf.videoView videoPlayView:weakSelf splitedScreenSelectVideoWithIndex:weakSelf.index];
        }
    }];
    
    //3.内外网地址
    PopoverAction *ipTypeAction = nil;
    if ([((NSArray *)self.streamArray.firstObject) count] > 1) {
        NSArray *streamArray = [self.streamArray objectAtIndexSafe:0];
        if ([streamArray isKindOfClass:[NSArray class]]) {
            VideoIpTypeModel *unselectModel = nil;
            for (int i = 0; i < streamArray.count; ++i) {
                VideoIpTypeModel *model = [streamArray objectAtIndexSafe:i];
                if (!model.isSelect) {
                    unselectModel = model;
                }
            }
            ipTypeAction = [PopoverAction actionWithTitle:unselectModel.showName handler:^(PopoverAction *action) {
                [weakSelf switchIpTypeWithModel:unselectModel];
            }];
        }
    }
    
    //4.主次码流
    PopoverAction *channelAction = nil;
    if ([((NSArray *)self.streamArray.lastObject) count] > 1) {
        NSArray *streamArray = [self.streamArray objectAtIndexSafe:1];
        if ([streamArray isKindOfClass:[NSArray class]]) {
            VideoStreamModel *unselectModel = nil;
            for (int i = 0; i < streamArray.count; ++i) {
                VideoStreamModel *model = [streamArray objectAtIndexSafe:i];
                if ([model isKindOfClass:[VideoStreamModel class]] && !model.isCurrentSelected) {
                    unselectModel = model;
                }
            }
            channelAction = [PopoverAction actionWithTitle:unselectModel.title?:@"次码流" handler:^(PopoverAction *action) {
                [weakSelf switchStramTypeWithModel:unselectModel];
            }];
        }
    }
    
    NSMutableArray *actionsArray = [NSMutableArray arrayWithObjects:playAction,selectAction, nil];
    if (ipTypeAction) {
        [actionsArray addObject:ipTypeAction];
    }
    if (channelAction) {
        [actionsArray addObject:channelAction];
    }
    return actionsArray;
}

#pragma mark - TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.streamArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = [self.streamArray objectAtIndexSafe:section];
    if ([array isKindOfClass:[NSArray class]]) {
        return array.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PlayRateTableViewCell *cell = [PlayRateTableViewCell cellWithTableView:tableView class:[PlayRateTableViewCell class]];
    NSArray *array = [self.streamArray objectAtIndexSafe:indexPath.section];
    id model = [array objectAtIndexSafe:indexPath.row];
    if ([model isKindOfClass:[VideoStreamModel class]]) {
        cell.title = ((VideoStreamModel *)model).title;
        cell.isCurrentSelected = ((VideoStreamModel *)model).isCurrentSelected;
        if (cell.isCurrentSelected) {
            self.toolBar.model = model;
        }
    }
    else if ([model isKindOfClass:[VideoIpTypeModel class]]) {
        cell.title = ((VideoIpTypeModel *)model).showName?:@"";
        cell.isCurrentSelected = ((VideoIpTypeModel *)model).isSelect;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 35.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return .5f;
    }
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footer = [UIView new];
    footer.backgroundColor = kWhiteColor;
    return footer;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self moveStreamTableView];
    
    NSArray *array = [self.streamArray objectAtIndexSafe:indexPath.section];
    id obj = [array objectAtIndexSafe:indexPath.row];
    
    if ([obj isKindOfClass:[VideoIpTypeModel class]]) {
        VideoIpTypeModel *model = (VideoIpTypeModel *)obj;
        
        if (model.isSelect) {
            return;
        }
        [self switchIpTypeWithModel:model];
        [self.tableView reloadData];
    }
    else if ([obj isKindOfClass:[VideoStreamModel class]]) {
        VideoStreamModel *model = (VideoStreamModel *)obj;
        
        if (model.isCurrentSelected) {
            return;
        }
        
        [self switchStramTypeWithModel:model];
        [self.tableView reloadData];
    }
}

- (void)switchIpTypeWithModel:(VideoIpTypeModel *)model {
    if (![model isKindOfClass:[VideoIpTypeModel class]]) {
        return;
    }
    
    if ([self.videoView.delegate respondsToSelector:@selector(videoView:videoPlayView:videoPlayIndex:streamTypeSelectWithIndexPath:stream:ipType:)]) {
        //赋值类型,切换内外网
        self.model.type = model.type;
        
        [self.videoView.delegate videoView:self.videoView videoPlayView:self videoPlayIndex:self.index streamTypeSelectWithIndexPath:nil stream:nil ipType:model];
        
        NSArray *array = [self.streamArray objectAtIndexSafe:0]?:@[];
        [array enumerateObjectsUsingBlock:^(VideoIpTypeModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.isSelect = NO;
        }];
        
        self.toolBar.ipType = model;
        
        model.isSelect = YES;
    }
}

- (void)switchStramTypeWithModel:(VideoStreamModel *)model {
    if (![model isKindOfClass:[VideoStreamModel class]]) {
        return;
    }
    
    if ([self.videoView.delegate respondsToSelector:@selector(videoView:videoPlayView:videoPlayIndex:streamTypeSelectWithIndexPath:stream:ipType:)]) {
        [self.videoView.delegate videoView:self.videoView videoPlayView:self videoPlayIndex:self.index streamTypeSelectWithIndexPath:nil stream:model ipType:nil];
        
        self.toolBar.model = model;
        
        NSArray *array = [self.streamArray objectAtIndexSafe:1]?:@[];
        [array enumerateObjectsUsingBlock:^(VideoStreamModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.isCurrentSelected = NO;
        }];
        
        model.isCurrentSelected = YES;
    }
}

- (void)setIndex:(int)index {
    _index = index;
    _toolBar.index = index;
}

- (void)setVideoView:(VideoView *)videoView {
    _videoView = videoView;
    _toolBar.videoView = videoView;
}

- (void)setPlayStatus:(PlayVideoStatus)playStatus {
    _playStatus = playStatus;
    switch (playStatus) {
        case PlayVideoStatusStop:
        {
            self.selectDeviceButton.hidden = YES;
            if (!self.videoView.isSplited) {
                self.playButton.hidden = NO;
            }
            [self.toolBar.playBtn setImage:ImageNamed_bb(@"monitoring_video_play_n_ic") forState:UIControlStateNormal];
            [self.toolBar.playBtn setImage:ImageNamed_bb(@"monitoring_video_play_p_ic") forState:UIControlStateHighlighted];
        }
            break;
        case PlayVideoStatusPause:
        {
            self.selectDeviceButton.hidden = YES;
            if (!self.videoView.isSplited) {
                self.playButton.hidden = NO;
            }
            [self.toolBar.playBtn setImage:ImageNamed_bb(@"monitoring_video_play_n_ic") forState:UIControlStateNormal];
            [self.toolBar.playBtn setImage:ImageNamed_bb(@"monitoring_video_play_p_ic") forState:UIControlStateHighlighted];
        }
            break;
        case PlayVideoStatusPlaying:
        {
            self.selectDeviceButton.hidden = YES;
            if (!self.videoView.isSplited) {
                self.playButton.hidden = YES;
            }
            [self.toolBar.playBtn setImage:ImageNamed_bb(@"monitoring_video_pause_n_ic") forState:UIControlStateNormal];
            [self.toolBar.playBtn setImage:ImageNamed_bb(@"monitoring_video_pause_p_ic") forState:UIControlStateHighlighted];
        }
            break;
        case PlayVideoStatusUnknown:
        default:
        {
            self.selectDeviceButton.hidden = NO;
            if (!self.videoView.isSplited) {
                self.playButton.hidden = YES;
            }
            [self.toolBar.playBtn setImage:ImageNamed_bb(@"monitoring_video_play_n_ic") forState:UIControlStateNormal];
            [self.toolBar.playBtn setImage:ImageNamed_bb(@"monitoring_video_play_p_ic") forState:UIControlStateHighlighted];
        }
            break;
    }

}

- (void)setIsFullScreen:(BOOL)isFullScreen {
    _isFullScreen = isFullScreen;
    self.videoView.isFullScreen = isFullScreen;
}

- (void)setIsShowToolBar:(BOOL)isShowToolBar {
    _isShowToolBar = isShowToolBar;
    if (isShowToolBar) {
        self.toolBar.hidden = NO;
        [self.toolBar mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(40);
        }];
    }
    else {
        [self.toolBar mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        self.toolBar.hidden = YES;
    }
}

- (void)setModel:(SelectVideoDeviceModel *)model {
    _model = model;
    self.streamArray = [NSMutableArray array];
    NSMutableArray *addressArray = [NSMutableArray array];
    if (model.outTypeModel) {
        [addressArray addObject:model.outTypeModel];
    }
    if (model.inTypeModel) {
        [addressArray addObject:model.inTypeModel];
    }
    [self.streamArray addObject:addressArray];
    
    VideoStreamModel *streamModel = [VideoStreamModel new];
    streamModel.title = @"主码流";
    streamModel.channel = model.startChan;
    streamModel.streamType = 0;
    streamModel.isCurrentSelected = YES;
    
    VideoStreamModel *streamModel1 = [VideoStreamModel new];
    streamModel1.title = @"次码流";
    streamModel1.channel = model.startChan;
    streamModel1.streamType = 1;
    streamModel1.isCurrentSelected = NO;
    
    NSMutableArray *array = [NSMutableArray arrayWithObjects:streamModel,streamModel1, nil];
    
    [self.streamArray addObject:array];

    [self.tableView reloadData];
    
}

- (UIView *)playContainer {
    if (!_playContainer) {
        _playContainer = [UIView new];
    }
    return _playContainer;
}

- (VideoToolBar *)toolBar {
    if (!_toolBar) {
        _toolBar = [VideoToolBar new];
        _toolBar.index = _index;
        _toolBar.videoView = _videoView;
        _toolBar.videoPlayView = self;
    }
    return _toolBar;
}

- (BMCustomButton *)selectDeviceButton {
    if (!_selectDeviceButton) {
        _selectDeviceButton = [BMCustomButton buttonWithType:UIButtonTypeCustom];
        _selectDeviceButton.customStyle = CustomButtonStyleImageTopTitleBottomCenter;
        [_selectDeviceButton setTitle:@"点击选择播放设备" forState:UIControlStateNormal];
        [_selectDeviceButton setImage:ImageNamed_bb(@"monitoring_video_add_n_ic") forState:UIControlStateNormal];
        [_selectDeviceButton setImage:ImageNamed_bb(@"monitoring_video_add_p_ic") forState:UIControlStateHighlighted];
        [_selectDeviceButton addTarget:self action:@selector(selectDeviceButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectDeviceButton;
}

- (UIButton *)playButton {
    if (!_playButton) {
        _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_playButton setImage:ImageNamed_bb(@"app_video_monitor_play_ic") forState:UIControlStateNormal];
        [_playButton addTarget:self action:@selector(playButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
        _playButton.hidden = YES;
    }
    return _playButton;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = Color_RGB(33, 41, 45);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


@end

#pragma mark VideoToolBar-----------------
@interface VideoToolBar ()

@end

@implementation VideoToolBar

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    self.clipsToBounds = YES;
    [self setupFuncContainer];
    return self;
}

- (void)setupFuncContainer {
    self.funcContainer = [UIView new];
    self.funcContainer.backgroundColor = Color_RGB(33, 41, 45);
    [self addSubview:self.funcContainer];
    [self.funcContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.playBtn setImage:ImageNamed_bb(@"monitoring_video_play_n_ic") forState:UIControlStateNormal];
    [self.playBtn setImage:ImageNamed_bb(@"monitoring_video_play_p_ic") forState:UIControlStateHighlighted];
    [self.playBtn addTarget:self action:@selector(playBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.funcContainer addSubview:self.playBtn];
    
    NSInteger padding = SCREEN_WIDTH > 370 ? 15 : 5;
    NSInteger btnW = SCREEN_WIDTH > 375 ? 50 : 40;
    
    [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo((padding+btnW)*0);
        make.width.mas_equalTo(btnW);
        make.top.bottom.equalTo(self.funcContainer);
    }];
    
    self.voiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.voiceBtn setImage:ImageNamed_bb(@"monitoring_video_mute_n_ic") forState:UIControlStateNormal];
    [self.voiceBtn setImage:ImageNamed_bb(@"monitoring_video_mute_n_ic") forState:UIControlStateHighlighted];
    [self.voiceBtn addTarget:self action:@selector(voiceBtnClick)  forControlEvents:UIControlEventTouchUpInside];
    [self.funcContainer addSubview:self.voiceBtn];
    [self.voiceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo((padding+btnW)*1);
        make.width.mas_equalTo(btnW);
        make.top.bottom.equalTo(self.funcContainer);
    }];
    
    self.fullScreenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.fullScreenBtn setImage:ImageNamed_bb(@"monitoring_video_full_n_ic") forState:UIControlStateNormal];
    [self.fullScreenBtn setImage:ImageNamed_bb(@"monitoring_video_full_n_ic") forState:UIControlStateHighlighted];
    [self.fullScreenBtn addTarget:self action:@selector(fullScreenBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.funcContainer addSubview:self.fullScreenBtn];
    [self.fullScreenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo((padding+btnW)*2);
        make.width.mas_equalTo(btnW);
        make.top.bottom.equalTo(self.funcContainer);
    }];
    
    self.splitScrrenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.splitScrrenBtn setImage:ImageNamed_bb(@"monitoring_video_screen1_n_ic") forState:UIControlStateNormal];
    [self.splitScrrenBtn setImage:ImageNamed_bb(@"monitoring_video_screen1_n_ic") forState:UIControlStateHighlighted];
    [self.splitScrrenBtn addTarget:self action:@selector(splitScrrenBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.funcContainer addSubview:self.splitScrrenBtn];
    [self.splitScrrenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo((padding+btnW)*3);
        make.width.mas_equalTo(btnW);
        make.top.bottom.equalTo(self.funcContainer);
    }];
    
    self.playRateBtn = [BMCustomButton buttonWithType:UIButtonTypeCustom];
    self.playRateBtn.customStyle = CustomButtonStyleImageRightTitleLeft;
    [self.playRateBtn setTitle:@"主码流 " forState:UIControlStateNormal];
    [self.playRateBtn setTitleColor:ColorDefalutGray forState:UIControlStateNormal];
    [self.playRateBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.playRateBtn setImage:ImageNamed_bb(@"app_arrow_ic") forState:UIControlStateNormal];
    [self.playRateBtn setImage:ImageNamed_bb(@"app_arrow_ic") forState:UIControlStateHighlighted];
    [self.playRateBtn addTarget:self action:@selector(playRateBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.funcContainer addSubview:self.playRateBtn];
    [self.playRateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.width.mas_equalTo(80);
        make.top.bottom.equalTo(self.funcContainer);
    }];
}

- (void)playBtnClick
{
    if ([self.videoView.delegate respondsToSelector:@selector(videoView:videoPlayView:clickPlayPauseButton:playStatus:)]) {
        [self.videoView.delegate videoView:self.videoView videoPlayView:(VideoPlayView *)self.superview clickPlayPauseButton:self.playBtn playStatus:((VideoPlayView *)self.superview).playStatus];
    }
}

- (void)voiceBtnClick
{
    if ([self.videoView.delegate respondsToSelector:@selector(videoView:videoPlayView:clickVoiceButton:isVoiceMuted:)]) {
        
        [self.videoView.delegate videoView:self.videoView videoPlayView:(VideoPlayView *)self.superview clickVoiceButton:self.voiceBtn isVoiceMuted:self.videoPlayView.isVoiceMuted];
        
        if (!self.videoPlayView.isVoiceMuted) {
            [self.voiceBtn setImage:ImageNamed_bb(@"monitoring_video_mute_n_ic") forState:UIControlStateNormal];
            [self.voiceBtn setImage:ImageNamed_bb(@"monitoring_video_mute_n_ic") forState:UIControlStateHighlighted];
        }
        else {
            [self.voiceBtn setImage:ImageNamed_bb(@"monitoring_video_volume_n_ic") forState:UIControlStateNormal];
            [self.voiceBtn setImage:ImageNamed_bb(@"monitoring_video_volume_n_ic") forState:UIControlStateHighlighted];
        }
    }
}

- (void)fullScreenBtnClick
{
    if ([self.videoView.delegate respondsToSelector:@selector(videoView:videoPlayView:clickFullScreenButton:isFullScreen:)]) {
        [self.videoView.delegate videoView:self.videoView videoPlayView:(VideoPlayView *)self.superview clickFullScreenButton:self.fullScreenBtn isFullScreen:self.videoPlayView.isFullScreen];
    }
}

- (void)splitScrrenBtnClick
{
    if ([self.videoView.delegate respondsToSelector:@selector(videoView:videoPlayView:clickSplitScreenButton:isSplitedScreen:)]) {
        [self.videoView.delegate videoView:self.videoView videoPlayView:self.videoPlayView clickSplitScreenButton:self.splitScrrenBtn isSplitedScreen:self.videoView.isSplited];
    }
}

- (void)playRateBtnClick
{
    if ([self.videoView.delegate respondsToSelector:@selector(videoView:videoPlayView:videoPlayIndex:clickStreamSelectButton:)]) {
        [self.videoView.delegate videoView:self.videoView videoPlayView:(VideoPlayView *)self.superview videoPlayIndex:self.index clickStreamSelectButton:self.playRateBtn];
    }
    
    if (self.BlockClickStreamBtn) {
        self.BlockClickStreamBtn();
    }
}

- (void)setModel:(VideoStreamModel *)model {
    _model = model;
    NSString *title = [NSString stringWithFormat:@"%@ ",model.title?:@"主码流"];
    [self.playRateBtn setTitle:title forState:UIControlStateNormal];
}

//- (void)setIpType:(VideoIpTypeModel *)ipType {
//    _ipType = ipType;
//    [self.playRateBtn setTitle:ipType.showName?:@"主码流" forState:UIControlStateNormal];
//}

@end

#pragma mark VideoView-----------------
@interface VideoView () <VideoViewDelegate>
@property (nonatomic, strong) UIButton *backButton;
@end

@implementation VideoView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    [self addSubview:self.backButton];
    
    [self setupSinglePlayView];
    
    return self;
}

- (void)setupSinglePlayView {
    self.isSplited = NO;

    if (self.playViewArray.count == 4) {
        for (NSInteger i = self.playViewArray.count-1; i > 0; --i) {
            VideoPlayView *playView = [self.playViewArray objectAtIndex:i];
            if ([self.delegate respondsToSelector:@selector(videoView:videoPlayView:stopOtherPlayWithSplitedStateIndex:)]) {
                [self.delegate videoView:self videoPlayView:playView stopOtherPlayWithSplitedStateIndex:(int)i];
            }
            [playView removePopover];
            playView.isShowToolBar = YES;
            [self.playViewArray removeObjectAtIndex:i];
            [playView removeFromSuperview];
        }
        
        VideoPlayView *playView = self.playViewArray.firstObject;
        if (playView) {
            [playView removePopover];
            playView.isShowToolBar = YES;
            [playView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self);
            }];
        }
        
    }
    else {
        [self.playViewArray makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self.playViewArray removeAllObjects];
        
        VideoPlayView *playView = [VideoPlayView new];
        [playView removePopover];
        playView.isShowToolBar = YES;
        playView.index = 0;
        playView.videoView = self;
        [self.playViewArray addObject:playView];
        [self addSubview:playView];
        [playView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
}

- (void)setupSplitedPlayView {
    if (self.isSplited) {
        return;
    }
    
    self.isSplited = YES;
    
    NSInteger count = [self videoPlayCount];
    CGFloat videoW = SCREEN_HEIGHT * 0.5;
    CGFloat videoH = SCREEN_WIDTH * 0.5;
    
    BOOL isFromSingle = NO;
    
    if (self.playViewArray.count == 1) {
        isFromSingle = YES;
        VideoPlayView *playView = self.playViewArray.firstObject;
        [playView addPopover];
        playView.isShowToolBar = NO;
        [playView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(0);
            make.width.mas_equalTo(videoW);
            make.height.mas_equalTo(videoH);
        }];
    }
    
    for (int i = 0; i < count; ++i) {
        if (isFromSingle && i == 0) {
            continue;
        }
        VideoPlayView *playView = [VideoPlayView new];
        playView.index = i;
        playView.videoView = self;
        playView.isShowToolBar = NO;
        [playView addPopover];
        [self.playViewArray addObject:playView];
        [self addSubview:playView];
        [playView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(videoW * (i%2));
            make.top.mas_equalTo(videoH * (i/2));
            make.width.mas_equalTo(videoW);
            make.height.mas_equalTo(videoH);
        }];
    }
    
}

- (VideoPlayView *)videoPlayViewWithIndex:(int)index {
    VideoPlayView *playView = [self.playViewArray objectAtIndexSafe:index];
    if (playView) {
        return playView;
    }
    return nil;
}

- (void)setPlayStatus:(PlayVideoStatus)status withIndex:(int)index {
    VideoPlayView *playView = [self.playViewArray objectAtIndexSafe:index];
    if (playView) {
        playView.playStatus = status;
    }
}


- (void)toFullScreenWithVideoPlayView:(VideoPlayView *)playView completion:(void(^)(BOOL finished))completion {
    self.transform = CGAffineTransformIdentity;
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    [self removeFromSuperview];
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    playView.isFullScreen = YES;
    
    [UIView animateWithDuration:0.7f animations:^{
        self.transform = CGAffineTransformMakeRotation(-M_PI_2);
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }completion:^(BOOL finished) {
        if (completion) {
            completion(finished);
        }
        self.backButton.hidden = NO;
        [self bringSubviewToFront:self.backButton];
        [self layoutIfNeeded];
    }];
}

- (void)toNormalScreenWithSuperView:(UIView *)superView videoPlayView:(VideoPlayView *)playView completion:(void(^)(BOOL finished))completion {
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    [self removeFromSuperview];
    [superView addSubview:self];
    playView.isFullScreen = NO;
    self.backButton.hidden = YES;
    [self bringSubviewToFront:self.backButton];
    [UIView animateWithDuration:0.7f animations:^{
        self.transform = CGAffineTransformIdentity;
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, kVideoViewH);
    }completion:^(BOOL finished) {
        if (completion) {
            completion(finished);
        }
        [self layoutIfNeeded];
    }];
}

- (void)toSplitedScreen {
    [self toFullScreenWithVideoPlayView:nil completion:^(BOOL finished) {
        
    }];
    
    [self setupSplitedPlayView];
    
    self.isFullScreen = YES;
    self.isSplited = YES;
    self.backButton.hidden = NO;
    [self bringSubviewToFront:self.backButton];
}

- (void)cancelSplitedScreen:(UIView *)superView {
    
    [self toNormalScreenWithSuperView:superView videoPlayView:nil completion:^(BOOL finished) {
        
    }];
    
    [self setupSinglePlayView];
    
    self.isFullScreen = NO;
    self.isSplited = NO;
    self.backButton.hidden = YES;
    [self bringSubviewToFront:self.backButton];
}

- (void)backButtonClick:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(cancelFullScreenWithVideoView:)]) {
        
        UIView *superView = [self.delegate cancelFullScreenWithVideoView:self];

        //退出全屏(分屏)
        if (self.isSplited && self.isFullScreen) {
            [self cancelSplitedScreen:superView];
        }
        else {
            [self toNormalScreenWithSuperView:superView videoPlayView:nil completion:nil];
        }
        
        for (VideoPlayView *playView in self.playViewArray) {
            if ([playView isKindOfClass:[VideoPlayView class]]) {
                playView.isFullScreen = NO;
            }
        }
    }

}

- (NSInteger)videoPlayCount {
    return self.isSplited?4:1;
}

- (BOOL)isPlayingWithPlayViewIndex:(NSInteger)index {
    return YES;
}

- (BOOL)isVoiceMutedWithPlayViewIndex:(NSInteger)index {
    return YES;
}

- (NSInteger)clickIndexWithCurrentVideoPlayView {
    return 0;
}

- (NSMutableArray *)playViewArray {
    if (!_playViewArray) {
        _playViewArray = [NSMutableArray array];
    }
    return _playViewArray;
}

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backButton setImage:ImageNamed_bb(@"app_back_n_ic") forState:UIControlStateNormal];
        [_backButton setImage:ImageNamed_bb(@"app_back_p_ic") forState:UIControlStateHighlighted];
        _backButton.hidden = YES;
        _backButton.frame = CGRectMake(10, 10, 40, 40);
        [_backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}
@end
